
package com.dc.contoller;

import com.alibaba.fastjson.JSON;
import com.dc.base.model.BaseModel;
import com.dc.service.MessageService;
import com.dc.service.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


@WebServlet(name ="MessageServlet" ,
        urlPatterns = {"/addMessage.do","/updateMessage.do",
                "/deleteMessage.do","/selectMessage.do","/selectMessageid.do"})
public class MessageContoller  extends HttpServlet  {
    @Override
    protected void doPost
            (HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //做业务逻辑
        String url=req.getRequestURI();
        url=url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
        System.out.println(url);
        Method method=null;
        try {
            method=getClass().getDeclaredMethod(url,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e){
            System.out.println("java反射失败");
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
    public void addMessage(HttpServletRequest req, HttpServletResponse resp){

        System.out.println("我是添加的servlet！");

    }
    public void selectMessage(HttpServletRequest req, HttpServletResponse resp){
        MessageService messageService=new MessageServiceImpl();
        BaseModel baseModel=new BaseModel();
        try {
            baseModel=messageService.selectMessage(null);
            String baseModelStr= JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("查询所有用户信息接口出错");
            e.printStackTrace();
        }

    }
    public void selectMessageid(HttpServletRequest req, HttpServletResponse resp){
        MessageService messageService=new MessageServiceImpl();
        BaseModel baseModel=new BaseModel();
        try {
            int id= Integer.valueOf(req.getParameter("message_id"));
            System.out.println(id+"ss");
            baseModel=messageService.selectMessageid(id);
            String baseModelStr= JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){

            System.out.println("查询个人用户信息接口出错");
            e.printStackTrace();
        }

    }

}